package org.chen.controller;

import lombok.extern.slf4j.Slf4j;
import org.chen.common.ApiResponse;
import org.chen.service.VerificationCodeService;
import org.chen.service.impl.VerificationCodeServiceImpl;
import org.chen.utils.ControllerMethodTitle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/verify")
public class VerificationCodeController {

    @Autowired
    private VerificationCodeServiceImpl codeService;

    @PostMapping("/send-code")
    @ControllerMethodTitle("执行send-code接口")
    public ResponseEntity<ApiResponse> sendCode(@RequestParam String email) {
        log.info("\n    [处理中], 程序正在处理请求逻辑=======>>"+email);
        try {
            codeService.sendVerificationCode(email);
            return ResponseEntity.ok(new ApiResponse(true, "Verification code sent.", null));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().body(new ApiResponse(false,e.getMessage(),null));
        }
    }

    @PostMapping("/verify-code")
    @ControllerMethodTitle("执行verify-code接口")
    public ResponseEntity<ApiResponse> verifyCode(@RequestParam String email, @RequestParam String code,@RequestParam String name,@RequestParam String type) {
        log.info("\n    [处理中], 程序正在处理请求逻辑=======>>"+email+"-->"+code+"-->"+name);
        return ResponseEntity.ok(codeService.verifyCode(email, code, name, type));
    }
}
